1692F - 3SUM - CodeForces Solution


brute force math *1300

Please click on ads to support us..

Python Code:


def solve():
    n = int(input())
    a = list(map(int, input().split()))
    b = [0] * 10
    for i in range(n):
        b[a[i] % 10] += 1
    ans = 'no'
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if (i + j + k) % 10 != 3:
                    continue
                b[i] -= 1
                b[j] -= 1
                b[k] -= 1
                if b[i] >= 0 and b[j] >= 0 and b[k] >= 0:
                    ans = 'yes'
                b[i] += 1
                b[j] += 1
                b[k] += 1
    print(ans)





def main():
    t = int(input())
    for _ in range(t):
        solve()


main()

C++ Code:

#include <bits/stdc++.h>
using namespace std;

void solve(vector<int> v)
{
    for (int i = 0; i < v.size() - 2; i++)
        {

            for (int j = i + 1; j < v.size() - 1; j++)
            {

                for (int k = j + 1; k < v.size(); k++)
                {

                    if ((v[i] + v[j] + v[k]) % 10 == 3)
                    {
                        cout << "YES" << endl;

                        return;
                    }
                }
            }
        }

        cout << "NO" << endl;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        map<int, int> mp;
        for (int i = 0; i < n; i++)
        {
            long long int data;
            cin >> data;
            int r = data % 10;
            mp[r]++;
        }
        map<int, int>::iterator it = mp.begin();

        // Iterate through the map and print the elements
        vector<int> v;
        while (it != mp.end())
        {

            if (it->second >= 3)
            {
                for (int i = 0; i < 3; i++)
                {
                    v.push_back(it->first);
                }
            }
            else if (it->second <= 2)
            {
                for (int i = 0; i < it->second; i++)
                {
                    v.push_back(it->first);
                }
            }

            ++it;
        }

        solve(v);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas